<?php

namespace interface\pda;

use verify;

class loginAppClass extends index
{
	
	/**
	 * 给个人用户设置默认设置
	 */
	private function setDefaultSetting()
	{
		//生成OA小程序销售设置
		if (self::total(array('table' => 'user_setting', 'where' => "stid = '" . self::$stid . "' and type = 'OA通知设置'")) == 0) {
			insert(array(
				'table' => 'user_setting',
				'field' => [
					'stid'  => self::$stid,
					'type'  => 'OA通知设置',
					'value' => json_encode(['message' => 1], 320),
				],
			));
		}
		
		//判断是否有销售过程统计设置
		if (self::total(array('table' => 'user_setting', 'where' => "stid = '" . self::$stid . "' and type = '销售过程统计'")) == 0) {
			insert(array(
				'table' => 'user_setting',
				'field' => [
					'stid'  => self::$stid,
					'type'  => '销售过程统计',
					'value' => json_encode(["board", "follow_client", "sale_funnel", "follow_type", "follow_rank", "visit_rank"], 320),
				],
			));
		}
		//判断是否有销售业绩统计设置
		if (self::total(array('table' => 'user_setting', 'where' => "stid = '" . self::$stid . "' and type = '销售业绩统计'")) == 0) {
			insert(array(
				'table' => 'user_setting',
				'field' => [
					'stid'  => self::$stid,
					'type'  => '销售业绩统计',
					'value' => json_encode(["board", "performance_goals", "payment", "contract_money", "kpi_analysis", "losing_analysis", "sign_ranking", "goal_achievement"], 320),
				],
			));
		}
		//判断是否有客户分析设置
		if (self::total(array('table' => 'user_setting', 'where' => "stid = '" . self::$stid . "' and type = '客户分析'")) == 0) {
			insert(array(
				'table' => 'user_setting',
				'field' => [
					'stid'  => self::$stid,
					'type'  => '客户分析',
					'value' => json_encode(["geographical", "industry_scale", "status_funnel", "channel_analysis", "customer_add", "customer_ranking", "conversion_rate"], 320),
				],
			));
		}
	}
	
	public function login()
	{
		$token  = $this->header['Auth'];
		$source = $this->header['Source'];
		if (!$token) {
			//走userId登录流程
			$this->resp['warn'] = '未找到token';
		} else {
			$stid = $token == -1 ? self::$stid : $this->rGet($token);
			if (!$stid) {
				$this->resp['warn'] = '登录凭证已过期，请重新登录';
			} else {
				$user = query('user', " stid = '$stid' ");
				if (!$user) {
					$this->resp['warn'] = '未找到此用户';
				} else {
					(int)$expireTime = para('expireTime') ?? 2; //登录有效期，单位小时
					$expireTime = 60 * 60 * $expireTime;
					$this->rSet($token, $user['stid'], $expireTime); // 设置redis
					$this->resp['data'] = [
						'token' => $token,
					];
					$logText            = match ($source) {
						'crm'        => $user['name'] . '登录成功【CRM-小程序】-token',
						'erp'        => $user['name'] . '登录成功【ERP-小程序】-token',
						'pda', 'PDA' => $user['name'] . '登录成功【ERP-PDA】-token',
						default      => $user['name'] . '登录成功【OA-小程序】-token',
					};
					$this->code         = 1;
					$this->logText(["type" => 'my', "text" => $logText], true);
				}
			}
		}
	}
	
	/**
	 * 账号密码登录
	 */
	public function loginPWD()
	{
		$data = [
			'userName' => $this->post['userName'] ?? '',
			'password' => $this->post['password'] ?? '',
			'source'   => $this->post['source'] ?? '',
		];
		// 注释
		$annotation = [
			'userName' => '用户名或手机号',
			'password' => '密码',
			'source'   => '来源',
			// 'deviceID'   => '设备ID',
		];
		// 规则
		$rules = [
			'userName' => 'required',
			'password' => 'required|min_len:6,max_len:20',
			'source'   => 'required',
		];
		// 校验是否符合规则
		$vt     = new verify();
		$result = $vt->setAliases($annotation)->validate($data, $rules)->isOk();
		if ($result === false) {
			$this->resp['warn'] = $vt->getErrs()[0]; // 多个规则提示最前面一条
		} else {
			$source = $data['source'];
			unset($data['source']);
			$user = query('user', " userName = '{$data['userName']}' ");
			if (static::isNull($user['uid']) || verifyPwd($data['password'], $user['password']) !== true) {
				$this->resp['warn'] = '登录失败，账号密码输入错误';
			} else {
				$token = md5s($user['stid']);
				(int)$expireTime = para('expireTime') ?? 2; //登录有效期，单位小时
				$expireTime = 60 * 60 * $expireTime;
				$this->rSet($token, $user['stid'], $expireTime); // 设置redis
				$this->resp['data'] = [
					'token' => $token,
				];
				$logText            = match ($source) {
					'crm'        => $user['name'] . '登录成功【CRM小程序】-token',
					'erp'        => $user['name'] . '登录成功【ERP小程序】-token',
					'pda', 'PDA' => $user['name'] . '登录成功【ERP-PDA】-userId',
					default      => $user['name'] . '登录成功【OA小程序】-token',
				};
				$this->setDefaultSetting();
				$this->code         = 1;
				$this->logText(["type" => 'my', "text" => $logText], true);
			}
		}
	}
}
